var {getLoginedUser, fnLoading, fnHideLoading, fnSuccess, fnFail} = require('../../utils/index')
import {get, lsGetStr, post} from '../../utils/request.js';
import { checkLoginAsyc } from '../../utils/util.js';
import _ from '../../utils/underscore.js';

let app = getApp();
// pages/urgent/index.js
Page({
  fnDetail(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../service-detail/index?id='+id,
    });
  },
 
    /**
     * 页面的初始数据
     */
    data: {
        setdata: [],
      cartItemId:"",
      page:1,
      cartItemnumber:"",
        ldm:true,
      discountNam:"",
      typeId:''
    },
  // buttonCar: function () {
  //   wx.navigateTo({
  //     url = '../cart/index'
  //   })
  // },
  bindconfirm:function(e){
    var that = this;
    var discountName = e.detail.value['search - input'] ? e.detail.value['search - input'] : e.detail.value;
    that.setData({
      discountNam: discountName
    })
    that.onLoad(that.data.options);
  },
    /**
     * 生命周期函数--监听页面加载
     */
    //点击购物车
    cart: function (e) {
      var id = e.target.dataset.id;
      this.setData({
        cartItemId: id,
        cartItemnumber: 1,
      });
      checkLoginAsyc()
        .then(function () {
          post({
            path: "/order/addShoppingcart",
            data: {
              offerId: id,
              number: 1,
              skey: lsGetStr('skey')
            }
          })
        })
        .then(function (res) {
          fnSuccess("加入成功....")
        })
        .catch(e => {
      
        })
        ;
    },
  bindGetUserInfo(e){
    let that =this;
    app.authSetting(e)
    .then(user=>{
      return post({
        path: "/order/addShoppingcart",
        data: {
          offerId: that.data.cartItemId,
          number: that.data.cartItemnumber,
          skey: lsGetStr('skey')
        }
      })
    })
      .then(function (res) {
       that.setData({
         loginBgFlag: false
       })
      })
    .catch(e=>{

    })
  },
    //点击购买
    aply: function (e) {
      var id = e.target.dataset.id;
      this.setData({
        cartItemId: id,
        cartItemnumber: 1,
      });
      checkLoginAsyc()
      .then(function(){
        post({
          path: "/order/addShoppingcart",
          data: {
            offerId: id,
            number: 1,
            skey: lsGetStr('skey'),
          }
        })
      })
        .then(function (res) {
          wx.switchTab({
            url: '../cart/index',
          })
        })
      .catch(e=>{
          
      })
      ;
     
    },
    onLoad: function (options) {
        var that = this;
        that.setData({
          typeId:options.type
        })
        if (options.type == 14225) {
            fnLoading("加载中......")
            get({
              path: "/offer/query?type1=901"
            }).then(function (res) {
                fnHideLoading()
              var r = res.list.map(function (item) {
                item.unit = item.unit ||'';
                return item;
              });
                that.setData({
                  objBy:[],
                  options: options,
                  setdata: r
                })
            })
        }
        // } else if (options.type == 14194) {
        //     fnLoading("加载中......")
        //     get({
        //         path: "/offer/queryPlain?type1=3&skey=" + lsGetStr('skey')
        //     }).then(function (res) {
        //         console.log(res);
        //         fnHideLoading()
        //         that.setData({
        //           objBy:[],
        //           options: options,
        //           setdata:   res.map(function (item) {
        //             item.unit = item.unit||'' ;
        //             return item;
        //           })
        //         })
        //     })
        // } else if (options.type == 14199 || options.type == 14203
        //   || options.type == 14204
        //   || options.type == 14205) {
        //     fnLoading("加载中......")
        //     get({
        //         path: "/offer/queryPlain?type1=4&skey=" + lsGetStr('skey') + "&categoryId=" + options.type
        //     }).then(function (res) {
               
        //         wx.setNavigationBarTitle({
        //             title: 
        //             options.type == 14199?'商标':(
        //                 options.type == 14204 ? '版权':(
        //                   options.type == 14203 ? '专利' :'知识产权交易'
        //                 )
        //             ),
        //         });
        //       console.log(_.pluck(res, 'status'));
        //       console.log(_.pluck(res, 'id'));
        //         let objBy  = _.groupBy(res,'tags');
        //         get({
        //           path: '/category1/list?type1=category-other-' + options.type
        //         })
        //         .then(function(res2){
        //           res2 = _.filter(res2, item => item.isShow == '1')
        //           fnHideLoading();
        //           res2.map(item=>{
        //             item.list = objBy[item.id];
        //             return item;
        //           })
        //           that.setData({
        //             options: options,
        //             ldm: true,
        //             setdata: res.map(function (item) {
        //               item.unit = item.unit || '';
        //               return item;
        //             }),
        //             objBy: res2
        //           })
        //         })
                
        //     })
        // } 
    },
  fetchMoreData: function (page) {
    var that = this, options = this.data.options;
    if (that.data.lastPage){
    return;
    }
    let type1 = options.type == 14185 ? 2 : (options.type == 14194 ? 3 : (options.type == 14199 ? 4 : ('4')));
    let queryStr = '';
    if (options.type == 14199 || options.type == 14203
      || options.type == 14204
      || options.type == 14205) {
      queryStr = "&categoryId=" + options.type
      }
   
    fnLoading("加载中......")
    get({
      path: "/offer/queryPlain?type1=" + type1 + 
      "&skey=" + lsGetStr('skey') + 
      queryStr+
        "&keyword=" + that.data.discountNam + 
      '&page=' + page
    }).then(function (res) {
      wx.stopPullDownRefresh();
      fnHideLoading();
      var resArr = res.map(function (item) {
        item.unit = item.unit || '';
        return item;
      });
      ;
      console.log(_.pluck(resArr,'status'));
      console.log(_.pluck(resArr, 'id'));
      let tmp = [], arrTmp = page == 1 ? resArr :(resArr);
      let objBy = that.data.objBy;

      if (options.type == 14199 || options.type == 14203
        || options.type == 14204
        || options.type == 14205){
        tmp = _.groupBy(arrTmp, 'tags');
        objBy.map(item=>{
          item.list = (item.list||[]).concat(tmp[item.id])
        })
      } else {
        arrTmp = that.data.setdata.concat(resArr);
      }
      that.setData({
        lastPage:resArr.length<15,
        options: options,
        page: page,
        objBy: objBy,
        setdata: arrTmp
      })
    })
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      console.log('sf')
      this.fetchMoreData(1);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      this.fetchMoreData(this.data.page+1);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})